#!/bin/bash
##定义了常用的字体效果及基础功能
##定义字体效果
#红色背景
red_bg='\e[41;33;1m'
#红色字体
red_zt='\e[1;31m'
#绿色背景
green_bg='\033[0;32;1m'
#绿色字体
green_zt='\e[1;32m'
#关闭效果
end='\033[0m'
#字体闪烁
ss='\033[05m'
#消隐
sy='\33[08m'
##常用语句定义区
#主要用于步骤开始前的信息展现
Entering(){
	echo -e "${green_bg}正在进入...${end}\n${red_zt}${1}...${end}"
}
#红色字体+绿色字体
red_z_green_z_info(){
	echo -e "${red_zt}${1}:${end}${green_zt}${2}${end}"
}
#检测是否输入
input_check(){
    #1参未需要设置的变量值[ $DB_PASS ]
    #2参为这个参数的备注信息[例如:数据库的root密码]
    #3参为一个随机/默认值,例如[ 000000 ]
    #4参为这个变量名,例如 [ DB_PASS ]
    if [[ -z "$1" ]];then
        echo -e "${red_zt}检测到${2}没有输入，已使用默认值[ ${3} ]${end}"
        eval `echo "$4=$3"`
    else
        eval `echo "$4=$1"`
    fi
}
#绿色字体
green_zt(){
	echo -e "${green_zt}${1}${end}"
}
#红色字体+绿色字体
red_z_green_z(){
	echo -e "${red_zt}${1}${end}${green_zt}${2}${end}${green_zt}${3}${end}"
}
#红色字体
red_z(){
	echo -e "${red_zt}${1}${end}"
}
#红色背景
red_bg(){
	echo -e "${red_bg}${1}${end}"
}
#红色字体+红色背景
red_zt_bg(){
	echo -e "${red_zt}${1}${end}${red_bg}${2}${end}"
}
#输入函数
input(){
	echo -e "${red_zt}${1}${end}"
	read $2
}
#检测运行结果
chek_status(){
    if [[ "$1" -eq '0' ]];then
        if [[  "$3" -eq 'q' ]];then
            return 0
        else
            red_z_green_z_info "$2" "成功"
        fi
        return 0
    else
        if [[  "$3" == 'q' ]];then
           red_bg "$2"
           exit 1
        fi
        status='true'
        while $status;do
            red_zt_bg "$2" "失败"
            err='n'
            input "是否停止脚本运行[当前脚本PID: $$]\n输入f忽略该提示" "err"
            if [[ "$err" == 'f' ]];then
                status='false'
                if [[ "$status" == 'false' ]];then
                    red_z "已跳过该错误:[${2}失败]"
                else
                    status='true'
                fi
            else
                status=true
            fi
        done
    fi
}
#关闭防火墙
disable_firealld(){
    systemctl stop firewalld  > /dev/null 2>&1
    systemctl disable firewalld > /dev/null 2>&1
    setenforce 0
	if [[ -f "/etc/selinux/config" ]];then
			sed -i 's/SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config
	fi
	if [[ -f "/etc/sysconfig/selinux" ]];then
			sed -i 's/SELINUX=.*/SELINUX=disabled/g' /etc/sysconfig/selinux
	fi
}

#服务安装
service_install(){
    for i in $*;do
        red_z_green_z_info "正在安装---->>>>" "${i}\t请耐心等待"
        red_z_green_z_info "当前脚本进程PID---->>>" "$$"
        if [[ "$display" == 'n' || "$display" == 'N' ]];then
            $install $i > /dev/null 2>&1
        else
            $install $i
        fi
        chek_status "$?" "${i}安装"
    done
}
#服务卸载
service_unstall(){
    input "即将卸载以下服务:\n$* \n是否全部卸载[y/n]" "re"
    for i in $*;do
        green_zt "$i"
    done
    if [[ "$re" == "y" || "$re" == "Y" ]];then
        for i in $*;do
            red_z_green_z_info "正在卸载---->>>>" "$i"
            $unstall $i
            chek_status "$?" "${i}卸载"
        done
    else
        input "是否自定义输入卸载[y/n]" "zre"
        if  [[ "$zre" == "y" || "$zre" == "Y" ]];then
            input "请输入需要卸载的包,并以空格分开" "sre"
            for i in $sre;do
                $unstall $i
            done
        else
            red_z "跳过卸载"
        fi
    fi
}
#服务重启并设置开机自启
service_start(){
    systemctl daemon-reload
    for i in $*;do
        systemctl restart $i
        chek_status "$?" "${i}服务启动"
        systemctl enable $i > /dev/null
        chek_status "$?" "${i}设置服务开机启动"
    done
}
#停止服务运行
service_stop(){
    for i in $*;do
        systemctl stop $i  > /dev/null 2>&1
        chek_status "$?" "${i}服务关闭"
        systemctl disable $i  > /dev/null 2>&1
        chek_status "$?" "${i}关闭开机启动"
    done
}
#检测服务是否安装
service_chek(){
    for i in $*;do
        red_z_green_z_info "正在检查" "$i"
        ser=`$local_search | grep $i`
        if [[ -n "$ser" ]];then
            input "检测到本机已安装[$i]相关服务,请选择一个操作\n[1]跳过此服务[默认选项]\n[2]卸载该服务" db_remove
            if [[ "$db_remove" == '2' ]];then
                service_unstall "$ser"
            else
                red_z_green_z_info "跳过卸载" "$i"
                db_init='y'
            fi
        fi
    done
}
##更新时间
update_time(){
    if [[ ! -e '/usr/bin/chronyc' ]];then
        service_install 'chrony'
        sed -i 's/^server/#server/g' /etc/chrony.conf
        chronyc add server time.windows.com
        chronyc add server ntp.aliyun.com
    fi
    if [[ -z "$time_up" ]];then
        chronyc add server time.windows.com
        timedatectl set-timezone Asia/Shanghai
        n=`chronyc sources` 
        chronyc sources
        if [[ "$?" == 0 && "$n" > 3 ]];then
            status=0
        fi
        chek_status "$status" "时间更新"
        time_up=y
    fi
}
#主要用于命令展现
cmd(){
    for i in $*;do
        red_z_green_z_info "正在执行" "$i"
        $i
        chek_status "$1" "执行"
    done
}
#检测网络连接情况
Check_Network(){
    red_z_green_z "正在进行网络检测..."
    ping 114.114.114.114 -c 3  > /dev/null
    chek_status "$?" "IPV4连接检测.." "q"
	ping baidu.com -c 3 > /dev/null
	chek_status "$?" "DNS域名解析检测..." 'q'
    network_status='ok'
}
#检测和设置当前系统IP
Check_IP(){
    iP_s=`hostname -I |  head  -n1 |  awk  -F  ' '  '{print NF}'`
    if [[ "$iP_s" -eq '1' ]];then
        local_ip=`hostname -I`
    else
        ip_list=`hostname -I`
        local_ip=`hostname -I | awk '{print $2}'`
        red_z_green_z_info "请选择一个业务IP,回车启用默认"  "[ $local_ip ]"
        for i in $ip_list;do
            red_z "$i"
        done
        read select_ip
        if [[ -n "$select_ip" ]];then
            for i in $local_ip;do
                if [[ "select" -eq "$i" ]];then
                    local_ip=$select_ip
                else
                    red_z_green_z_info "输入的IP地址与本机IP地址不符，已启用默认IP"
                    
                fi
            done
        else
            red_z_green_z_info "输入的IP地址与本机IP地址不符，已启用默认IP[ $local_ip ]"
        fi
    fi
}
##加载函数
Loading_function(){
	for i in $*;do
		if [[ -f "$i" ]];then
            source $i
            chek_status "$?" "加载功能文件[ $i ]" 'q'
		else
			red_bg "找不到功能文件[ $i ]"
		fi
	done
    green_zt "全部功能加载完成"
}
item='yes'
